﻿using SqlSugar;

namespace Link_eLab.Redis.Base
{
    public class BaseRedis<T> : IBaseRedis<T> where T : class, new()
    {
        /// <summary>
        /// RedisGet
        /// </summary>
        /// <param name="key"></param>
        /// <returns>JSON结果集</returns>
        public T RedisGet(string key)
        {
            using SqlSugarClient dbClient = DbInstance.GetInstance();
            //ServiceStackRedis cache = (ServiceStackRedis)dbClient.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
            RedisCache cache = (RedisCache)dbClient.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
            return cache.Get<T>(key);
        }

        /// <summary>
        /// RedisSet
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns>JSON结果集</returns>
        public void RedisSet(string key, T value)
        {
            using SqlSugarClient dbClient = DbInstance.GetInstance();
            RedisCache cache = (RedisCache)dbClient.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
            cache.Add(key, value);
        }

        /// <summary>
        /// RedisSet
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="cacheDurationInSeconds"></param>
        /// <returns>JSON结果集</returns>
        public void RedisSet(string key, T value, int cacheDurationInSeconds)
        {
            using SqlSugarClient dbClient = DbInstance.GetInstance();
            RedisCache cache = (RedisCache)dbClient.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
            cache.Add(key, value, cacheDurationInSeconds);
        }

        /// <summary>
        /// RedisDel
        /// </summary>
        /// <param name="key"></param>
        /// <returns>JSON结果集</returns>
        public void RedisDel(string key)
        {
            using SqlSugarClient dbClient = DbInstance.GetInstance();
            RedisCache cache = (RedisCache)dbClient.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
            cache.Del(key);
        }

    }
}